import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/up-form.vue"; //弹框form
import {
    saveOutsourcedReport, //保存
    getOutsourcedReportByFid, //详情
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            positionData: [],
            positionData1: [],
            selected: [], //多选人
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            formData: {
                fId: "", //必传
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fReportnumber: "", //报告编号
                fReportdates: this.common.day(0), //报告日期
                fReportfileurl: "", //报告文件
                fReportstatus: "", //报告状态
                fReportresult: "", //报告判定
                fNonresult: "", //不合格参数及结果
                fRemark: "" ,//报告备注
                fCompanyid:"",
                fChecktype:"",

            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selectPostionObj: [],
            types: [],
            // 材料弹框
            materialRow: {},
            fCompanynames: null,
            fCompanyids: null,
            fFileurllist: [], //附件
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    this.getDetail({
                        fId: this.currentDetail.fId
                    })
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal
                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal
                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true
        if (this.statusNum == 1) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[2].disabled = true;
            this.formObj[3].disabled = true;
            this.formObj[4].disabled = true;
            this.formObj[5].disabled = true;
            this.formObj[8].uploadSelect = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[8].uploadSelect = false
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
            if (b == "fReportresult") { //报告判定
                if (data == '合格') {
                    this.formObj[10].disabled = true;
                    this.formObj[10].rules[0].required = false;
                } else if (data == '不合格') {
                    this.formObj[10].disabled = false;
                    this.formObj[10].rules[0].required = true;
                }
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fFileurllist.length; index++) {
                if (file.uid == this.fFileurllist[index].uid) {
                    if (this.fFileurllist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].atta.fId,
                        });
                    }
                    this.fFileurllist.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getOutsourcedReportByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClOutsourcedtesting
                    this.formData = data
                    this.formData.fReportfileurl = data.fReportfileurl
                    if (data.fReportdates == null || data.fReportdates == "") {
                        this.formData.fReportdates = this.common.day(0);
                    } else {
                        this.formData.recordfiles = []
                        this.fFileurllist = res.data.fReportfileurllist;
                        if (this.statusNum == 7) {
                            this.btnShow = false;
                            this.formObj[10].disabled = true;
                                this.formObj[10].rules[0].required = false;
                        } else {
                            this.btnShow = true;
                            if (data.fReportresult == '合格') {
                                this.formObj[10].disabled = true;
                                this.formObj[10].rules[0].required = false;
                            } else if (data.fReportresult == '不合格') {
                                this.formObj[10].disabled = false;
                                this.formObj[10].rules[0].required = true;
                            }
                        }
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
                if (this.statusNum == 7) {
                    this.btnShow = false;
                } else {
                    this.btnShow = true;
                }
            });
        },
        yj_saveForm() {
            this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(obj) {
            var data = {
                fId: obj.fId, //必传
                userId: obj.userId, //当前登录人id
                userName: obj.userName, //当前登录人姓名
                recordfiles: obj.recordfiles, //删除文件id集合
                fReportnumber: obj.fReportnumber, //报告编号
                fReportdates: obj.fReportdates, //报告日期
                fReportfileurl: obj.fReportfileurl, //报告文件
                fReportstatus: obj.fReportstatus, //报告状态
                fReportresult: obj.fReportresult, //报告判定
                fNonresult: obj.fNonresult, //不合格参数及结果
                fRemark: obj.fRemark, //报告备注
                fCompanyid:obj.fCompanyid,
                fChecktype:obj.fChecktype,

            }
            this.loading = true;
            this.isLock = true
            saveOutsourcedReport(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
        },
    },
};